package Exercise;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class UDPSendExercise02 {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(8887);
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入问题:>");
        String question = scanner.next();
        byte[] bytes = question.getBytes();
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(),8888);
        socket.send(packet);

        byte data[] = new byte[1024];
        packet = new DatagramPacket(data,data.length);
        socket.receive(packet);
        byte[] data1 = packet.getData();
        int length = packet.getLength();
        String s = new String(data1, 0, length);
        System.out.println(s);

        socket.close();

    }
}
